package com.bawei.es.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsIndexService;
import com.bawei.system.domain.dto.ReindexRequestDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.script.Script;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esIndex")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsIndexController {

    @Autowired
    private EsIndexService esIndexService;

    /**
     * 根据索引名称创建索引 settings
     * @return
     */
    @GetMapping("/creates")
    @ApiOperation(value = "根据索引名称创建索引 settings")
    public R<Boolean> indexCreate(String indexName, @RequestParam(defaultValue = "{}") String settingsJson) {
        Map<String, Object> settings = JSON.parseObject(settingsJson, new TypeReference<Map<String, Object>>() {});
        return esIndexService.indexCreates(indexName,settings);
    }

    /**
     * 根据索引名称、映射结构创建索引
     *
     * @return
     */
    @GetMapping("/createWithMapping")
    @ApiOperation(value = "根据索引名称、映射结构创建索引")
    public R<Boolean> indexCreateWithMapping(String indexName, String indexMapping) {

        return esIndexService.indexCreateWithMapping(indexName,indexMapping);
    }

    /**
     * 根据索引名称查询索引是否存在
     *
     * @return
     */
    @GetMapping("/indexExit")
    @ApiOperation(value = "根据索引名称查询索引是否存在")
    public R<Boolean> indexExit(String indexName) {
        return esIndexService.indexExit(indexName);
    }

    /**
     * 根据索引名称删除索引
             *2.添加一个删除索引的接口
     * @return
     */
    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Boolean> indexDelete(String indexName) {
        return esIndexService.indexDelete(indexName);
    }

    /**
     *根据一个根据索引名称查询详情（返回setting、mapping）
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称获取setting,mapping")
    public R<Map<String,Object>> indexGetMapping(String indexName) {
        return esIndexService.indexGetMapping(indexName);
    }

    //4.添加一个修改索引接口，要可以修改已有的mappings映射，比如增加一个字段、删除一个字段等
    @PostMapping("/reindex")
    @ApiOperation(value = "修改索引,修改已有的mappings映射")
    public ResponseEntity<?> reindex(@RequestBody ReindexRequestDto dto) {
        try {
            // 假设ReindexRequestDTO包含了sourceIndex, targetIndex和可能的script字段
            Script script = null; // 根据需要构建Script对象，如果DTO中提供了相关信息
            // ...
            esIndexService.reindexWithNewFields(dto.getSourceIndex(), dto.getTargetIndex(), script);
            return ResponseEntity.ok("Reindex completed successfully.");
        } catch (IOException e) {
            // 处理异常，例如返回错误响应
            return ResponseEntity.status(500).body("Error during reindex: " + e.getMessage());
        }
    }

    /**
     * 查询所有索引
     */
    @GetMapping("/findIndex")
    @ApiOperation(value = "查询所有索引")
    public R<List<String>> findIndex(){
        List<String> list= esIndexService.findIndex();
        return R.ok(list);
    }
}
